package org.lan.tree;

import java.util.*;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/11/25 10:24 AM
 */
public class Node {

    public int val;

    public List<Node> children;

    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> list = new ArrayList<>();
        Deque<Node> deque = new ArrayDeque<>();
        deque.add(root);
        while (!deque.isEmpty()) {
            int len = deque.size();
            List<Integer> list1 = new ArrayList<>(len);
            for (int i = 0; i < len; i++) {
                Node node = deque.pop();
                if (node.children != null) {
                    for (Node child : node.children) {
                        deque.add(child);
                    }
                }
                list1.add(node.val);
            }
            list.add(list1);
        }
        return list;
    }

    public List<Integer> postorder(Node root) {
        List<Integer> list = new ArrayList<>();
        if (root != null && root.children != null) {
            for (Node child : root.children) {
                list.addAll(postorder(child));
            }
            list.add(root.val);
        }
        return list;
    }


}
